package com.jjw.route.service.impl;

import com.jjw.route.config.LoginUserInterceptor;
import com.jjw.route.vo.PortraitVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jjw.route.utils.PageUtils;
import com.jjw.route.utils.Query;

import com.jjw.route.dao.PortraitDao;
import com.jjw.route.entity.PortraitEntity;
import com.jjw.route.service.PortraitService;


@Service("portraitService")
public class PortraitServiceImpl extends ServiceImpl<PortraitDao, PortraitEntity> implements PortraitService {
    @Autowired
    PortraitDao portraitDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PortraitEntity> page = this.page(
                new Query<PortraitEntity>().getPage(params),
                new QueryWrapper<PortraitEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public boolean articalUpdatePreferenceDegree(float preferenceDegree,int poiTypeId,int uid) {
        preferenceDegree = preferenceDegree >= 1 ? preferenceDegree : -preferenceDegree;
        PortraitEntity portraitEntity = portraitDao.selectOne(new QueryWrapper<PortraitEntity>()
                .eq("uid", uid));
        String preference = portraitEntity.getPreference();
        String[] split = preference.split(",");
        split[poiTypeId] = String.valueOf(Double.parseDouble(split[poiTypeId]) + preferenceDegree);
        portraitEntity.setPreference(StringUtils.join(split,","));
        portraitDao.updateById(portraitEntity);
        return true;
    }

    @Override
    public PortraitVo getMyPortraitEntity(){
        PortraitEntity portraitEntity = portraitDao.selectOne(new QueryWrapper<PortraitEntity>()
                .eq("uid", LoginUserInterceptor.loginUser.get().intValue()));
        int[] preference = Arrays.stream(portraitEntity.getPreference().split(","))
                .mapToInt(Integer::parseInt).toArray();
        int[] models = Arrays.stream(portraitEntity.getMedal().split(","))
                .mapToInt(Integer::parseInt).toArray();
        return new PortraitVo(preference,models);
    }
}